# -*- coding: utf-8 -*-

# 第三方库
from pymongo import MongoClient
from scrapy.conf import settings


class SclSpiderPipeline(object):

    def __init__(self):
        # 连接到MongoDB数据库
        # _conn = MongoClient(host=settings['MONGODB_HOST'],
        #                     port=settings['MONGODB_PORT'],
        #                     username=settings['MONGODB_USER'],
        #                     password=settings['MONGODB_PWD'])
        _conn = MongoClient(host=settings['MONGODB_HOST'],
                            port=settings['MONGODB_PORT'])

        # 创建MongoDB数据库名称(数据源)
        self.db = _conn[settings['MONGODB_NAME']]

    # 管道加载
    def process_item(self, item, spider):
        # 独立管道入口
        if spider.name in ["school_library", 'GZ_Lib']:
            self.spider_pipeline(data=item, collection_name="tb_library")
            self.pipeline_change_status(spider_name=spider.name, name=item['searchName'])
        elif spider.name in ["Juejin", "Jianshu", "SegmentFault"]:
            self.spider_pipeline(data=item, collection_name="tb_article")
            self.pipeline_change_status(spider_name=spider.name, name=item['searchName'])
        return item

    def spider_pipeline(self, data, collection_name):
        # 集合对象
        col = self.db[collection_name]
        try:
            col.insert(data)
        except Exception as err:
            print(err)

    def pipeline_change_status(self, spider_name, name):
        # 集合对象
        col = self.db['tb_spiderStatus']
        try:
            col.update({"spiderName": spider_name}, {"$set": {"status": True}})
        except Exception as err:
            print(err)
